<style scoped src="./index.scss" lang="scss" rel="stylesheet/scss"></style>
<template src="./index.html"></template>
<script>
export default {
  mixins: [Vue.prototype.$vFN.Mixins.Base, Vue.prototype.$vFN.Mixins.Edit],
  components: {},
  data() {
    return {
      appWindow: window,
      form: {
        discountType: 1, //折扣方式 暂时只有一种 写死
        discountValue: "", //折扣金额
      },

      // 页面接口地址
      apiUrl: {
        detail: "api/time-discount/getDiscountProductItem",
        update: "api/time-discount/updateDiscountProduct",
      },

      productName: "",

      isloading: false,
    };
  },
  methods: {
    // 初始化
    async init() {
      console.error("id", this.ids);
      console.log("ss", this.type);

      if (this.type == "single") {
        this.getSingleData();
      }
      // 如果是批量，先处理传进来的id数组
      else if (this.type == "batch") {
        this.ids = this.ids.map((item) => {
          return item.id;
        });
        console.log("newIds", this.ids);
      }
      this.mixinEdit_editTitle = "修改";
      this.mixinEdit_editVisible = true;
    },

    async getSingleData() {
      this.isloading = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.detail,
          data: {
            id: this.ids[0],
          },
        },
        window
      );

      this.isloading = false;

      let data_ = res.data;

      for (const key in this.form) {
        if (!this.$vFN.Utils.isEmpty(data_[key]))
          this.form[key] = res.data[key];
      }

      this.productName = data_.productName;
    },

    // 输入框校验
    validator(rule, value, callback) {
      if (value > 10) {
        callback(new Error("最大折扣为10"));
      } else if (value === 0) {
        callback(new Error("最小折扣不能为0"));
      } else {
        this.$vFN.Tools.validateMoney(rule, value, callback);
      }
    },

    validate() {
      let that = this;
      return new Promise((resolve, reject) => {
        that.$refs["editForm"].validate((valid) => {
          if (valid) {
            resolve(true);
          } else {
            resolve(false);
          }
        });
      });
    },

    async submit() {
      if (!(await this.validate())) {
        return;
      }

      this.isloading = true;
      let res = await this.$vFN.Http.post(
        {
          url: this.apiUrl.update,
          data: {
            ...this.form,
            timeDiscountProductIdLst: this.ids,
          },
        },
        window
      );

      this.isloading = false;

      if (res._ok) {
        this.$vFN.Tools.messageSuccess(res.message);
        this.mixinEdit_editVisible = false;
      }
    },
  },
  mounted() {
    this.init();
  },
  activated() {},
};
</script>
